using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    internal class FormNumber : CMFormElement
    {
        public override string Value
        {
            get
            {
                TextBox textbox = this.FindControl(string.Format("textbox_{0}", this.ID)) as TextBox;
                return textbox.Text;
            }
        }

        internal FormNumber(Forms.Sdk.Items.Number element) : base(element)
        {
            System.Web.UI.WebControls.TextBox textbox = new TextBox();
            textbox.ID = string.Format("textbox_{0}", this.ID);

            if (element.DefaultValue.HasValue)
            {
                textbox.Text = element.DefaultValue.Value.ToString();
            }

            textbox.CssClass = string.Format("{0}Input", element.CSS);

            Label label = new Label();
            label.Text = element.Label;
            label.AssociatedControlID = textbox.ID;
            label.CssClass = string.Format("{0}Label", element.CSS);

            #region Validators

            PlaceHolder phValidators = new PlaceHolder();

            if (element.Required)
            {
                Label redStar = new Label();
                redStar.ForeColor = Color.Red;
                redStar.Text = "*";

                RequiredFieldValidator required = new RequiredFieldValidator();
                required.ID = string.Format("required_{0}_{1}", element.FormID, element.ID);
                required.ControlToValidate = textbox.ID;
                required.ValidationGroup = element.FormID.ToString();
                required.Display = ValidatorDisplay.Dynamic;
                required.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label); 

                phValidators.Controls.Add(redStar);
                phValidators.Controls.Add(required);
            }

            //Regular expression
            RegularExpressionValidator regular = new RegularExpressionValidator();
            regular.ID = string.Format("regular_{0}", this.ID);
            regular.ControlToValidate = textbox.ID;
            regular.ValidationGroup = element.FormID.ToString();
            regular.Display = ValidatorDisplay.Dynamic;
            regular.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("NotValid"), element.Label);

            switch (element.Type)
            {
                case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Float:
                    regular.ValidationExpression = "[0-9]*.[0-9]*";
                    break;
                case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Money:
                    regular.Enabled = false;
                    break;
                case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Int:
                default: regular.ValidationExpression = "[0-9]*";
                    break;
            }

            phValidators.Controls.Add(regular);

            //Range (Min & Max)
            RangeValidator range = new RangeValidator();
            range.ID = string.Format("range_{0}", this.ID);
            range.ControlToValidate = textbox.ID;
            range.ValidationGroup = element.FormID.ToString();
            range.Display = ValidatorDisplay.Dynamic;

            switch (element.Type)
            {
                case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Int:
                    {
                        range.Type = ValidationDataType.Integer;
                    } break;
                case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Float:
                    {
                        range.Type = ValidationDataType.Double;
                    } break;
                case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Money:
                    {
                        range.Type = ValidationDataType.Currency;
                    } break;
                default: break;
            }

            //Minimum value
            if (element.MinValue.HasValue)
            {
                range.MinimumValue = element.MinValue.Value.ToString();
            }
            else
            {
                switch (element.Type)
                {
                    case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Int:
                        {
                            range.MinimumValue = int.MinValue.ToString();
                        } break;
                    case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Float:
                    case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Money:
                        {
                            range.MinimumValue = double.MinValue.ToString();
                        } break;
                    default: break;
                }
            }
            
            //Maximum value
            if (element.MaxValue.HasValue)
            {
                range.MaximumValue = element.MaxValue.Value.ToString();
            }
            else
            {
                switch (element.Type)
                {
                    case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Int:
                        {
                            range.MaximumValue = int.MaxValue.ToString();
                        } break;
                    case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Float:
                    case Alogient.Cameleon.Forms.Sdk.Items.NumberType.Money:
                        {
                            range.MaximumValue = double.MaxValue.ToString();
                        } break;
                    default: break;
                }
            }
            
            if (element.Type == Alogient.Cameleon.Forms.Sdk.Items.NumberType.Money)
                range.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("NotValidMoney"), element.Label, range.MinimumValue, range.MaximumValue, string.Format("123{0}456{1}78", System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyGroupSeparator, System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator)); 
            else
                range.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("NotValidBetween"), element.Label, range.MinimumValue, range.MaximumValue); 

            
            phValidators.Controls.Add(range);

            #endregion

            GenerateRow(element, label, textbox, phValidators);
        }
    }
}
